from domain.domain import TeachingScience
from service.Message import Message,CodeMessageData
import re

class TeachingScienceDAO():
    def __init__(self):
        print("TeachingScienceDAO init")

    def add(self, session, oneTeachingScience):
        """
        :param session: session
        :param oneTeachingScience: one TeachingScience data
        :return: Message Object
        """
        data = []
        try:
            session.add(oneTeachingScience)
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

        # 一次提交多个数据

    def addAll(self, session,TeachingScienceList):
        try:
            session.add_all(TeachingScienceList)
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])


    def deleteById(self, session, id):
        """
        :param session: session
        :param id: personalTeamid
        :return: result Message Object
        """
        data = []
        try:
            session.query(TeachingScience).filter(TeachingScience.id == id).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    def deleteByJobNumberAndIdlist(self, session,jobNumber, idList, adminFlag=0):
        """
              :param session: session
              :param id: personalTeamid
              :return: result Message Object
              """
        data = []
        try:
            # session.query(PersonalTeam).filter(PersonalTeam.id._in(id)).delete()
            if(adminFlag):
                for each in idList:
                    session.query(TeachingScience).filter(TeachingScience.id == each).delete()
            else:
                for each in idList:
                    session.query(TeachingScience).filter(TeachingScience.id == each).filter(TeachingScience.job_number == jobNumber).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    def getAllInfo(self, session,jobNumber,DHFlag=0):
        """
        get all info
        :param session: sql session
        :return:
        """
        allData = []
        try:
            if (DHFlag == 1):
                list = session.query(TeachingScience).all()
            else:
                list = session.query(TeachingScience).filter(TeachingScience.job_number == jobNumber).all()
            code = 0000
            for each in list:
                allData.append(each.toDict())
        except:
            code = 1008
        return Message(code, CodeMessageData[code], data=allData)

    def updateByJobNumberAndDataList(self, session,jobNumber, newData,adminFlag=0):
        code = 0000
        try:
            for one in newData:
                if(adminFlag):
                    part = session.query(TeachingScience).filter(TeachingScience.id == one.id).one()
                else:
                    part = session.query(TeachingScience).filter(TeachingScience.id == one.id).filter(TeachingScience.job_number == jobNumber).one()
                part.id = one.id
                part.job_number = one.job_number
                part.result_name = one.result_name
                part.result_form = one.result_form
                part.units_issued = one.units_issued
                part.award_type = one.award_type
                part.award_grade = one.award_grade
                part.award_year = one.award_year
                part.order = one.order
                part.total_people = one.total_people
            session.commit()
        except Exception as ex:
            code = 1015
            session.rollback()
        return Message(code, CodeMessageData[code])

    def getDataByJobNumberAndIdList(self, session, jobNumber, idList, adminFlag=0):
        code = 0000
        data=[]
        try:
            for one in idList:
                if (adminFlag):
                    part = session.query(TeachingScience).filter(TeachingScience.id == one).one()
                else:
                    part = session.query(TeachingScience).filter(TeachingScience.id == one).filter(
                        TeachingScience.job_number == jobNumber).one()
                data.append(part.toDict())
        except Exception as ex:
            data=[]
            code = 1028
        return Message(code, CodeMessageData[code],data)


    def getName(self, session, id):
        code = 0000
        data = []
        try:
            result = session.query(TeachingScience).filter(TeachingScience.id == id).one()
            data.append(result.result_name)
        except:
            code = 1047
            data = []
        return Message(code, CodeMessageData[code], data=data)